<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="	 标准号" prop="标准号">
        <el-input
          v-model="queryParams.标准号"
          placeholder="请输入	 标准号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="	 冻精号" prop="冻精号">
        <el-input
          v-model="queryParams.冻精号"
          placeholder="请输入	 冻精号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="	 品种" prop="品种">
        <el-input
          v-model="queryParams.品种"
          placeholder="请输入	 品种"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="国别" prop="国别">
        <el-input
          v-model="queryParams.国别"
          placeholder="请输入国别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="	 TPI" prop="TPI">
        <el-input
          v-model="queryParams.TPI"
          placeholder="请输入	 TPI"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="	 冻精类型" prop="冻精类型">
        <el-input
          v-model="queryParams.冻精类型"
          placeholder="请输入	 冻精类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="	 数量" prop="数量">
        <el-input
          v-model="queryParams.数量"
          placeholder="请输入	 数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="	 购入日期" prop="购入日期">
        <el-date-picker clearable
          v-model="queryParams.购入日期"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择	 购入日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:djgl:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:djgl:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:djgl:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:djgl:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="djglList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="	 标准号" align="center" prop="标准号" />
      <el-table-column label="	 冻精号" align="center" prop="冻精号" />
      <el-table-column label="	 品种" align="center" prop="品种" />
      <el-table-column label="国别" align="center" prop="国别" />
      <el-table-column label="	 TPI" align="center" prop="TPI" />
      <el-table-column label="	 冻精类型" align="center" prop="冻精类型" />
      <el-table-column label="	 数量" align="center" prop="数量" />
      <el-table-column label="	 购入日期" align="center" prop="购入日期" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.购入日期, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:djgl:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:djgl:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改冻精管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="	 标准号" prop="标准号">
          <el-input v-model="form.标准号" placeholder="请输入	 标准号" />
        </el-form-item>
        <el-form-item label="	 冻精号" prop="冻精号">
          <el-input v-model="form.冻精号" placeholder="请输入	 冻精号" />
        </el-form-item>
        <el-form-item label="	 品种" prop="品种">
          <el-input v-model="form.品种" placeholder="请输入	 品种" />
        </el-form-item>
        <el-form-item label="国别" prop="国别">
          <el-input v-model="form.国别" placeholder="请输入国别" />
        </el-form-item>
        <el-form-item label="	 TPI" prop="TPI">
          <el-input v-model="form.TPI" placeholder="请输入	 TPI" />
        </el-form-item>
        <el-form-item label="	 冻精类型" prop="冻精类型">
          <el-input v-model="form.冻精类型" placeholder="请输入	 冻精类型" />
        </el-form-item>
        <el-form-item label="	 数量" prop="数量">
          <el-input v-model="form.数量" placeholder="请输入	 数量" />
        </el-form-item>
        <el-form-item label="	 购入日期" prop="购入日期">
          <el-date-picker clearable
            v-model="form.购入日期"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择	 购入日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDjgl, getDjgl, delDjgl, addDjgl, updateDjgl } from "@/api/system/djgl";

export default {
  name: "Djgl",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 冻精管理表格数据
      djglList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        标准号: null,
        冻精号: null,
        品种: null,
        国别: null,
        TPI: null,
        冻精类型: null,
        数量: null,
        购入日期: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询冻精管理列表 */
    getList() {
      this.loading = true;
      listDjgl(this.queryParams).then(response => {
        this.djglList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        标准号: null,
        冻精号: null,
        品种: null,
        国别: null,
        TPI: null,
        冻精类型: null,
        数量: null,
        购入日期: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.标准号)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加冻精管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const 标准号 = row.标准号 || this.ids
      getDjgl(标准号).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改冻精管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.标准号 != null) {
            updateDjgl(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDjgl(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const 标准号s = row.标准号 || this.ids;
      this.$modal.confirm('是否确认删除冻精管理编号为"' + 标准号s + '"的数据项？').then(function() {
        return delDjgl(标准号s);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/djgl/export', {
        ...this.queryParams
      }, `djgl_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
